<template>
    <div class="login-register">
        <el-container class="contain">
            <div class="big-box" :class="{ active: isLogin }">
                <div class="big-contain" key="bigContainLogin" v-if="isLogin">
                    <div class="btitle">账户登录</div>
                    <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
                        <div class="bform">
                            <el-form-item prop="username">
                                <el-input class="elin" placeholder="用户名" v-model="ruleForm.username"></el-input>
                            </el-form-item>
                            <el-form-item prop="password">
                                <el-input class="elin" placeholder="密码" v-model="ruleForm.password" show-password>
                                </el-input>
                            </el-form-item>
                        </div>
                        <el-button class="bbutton" @click="submitForm('ruleForm')">登录</el-button>
                    </el-form>
                </div>
                <div class="big-contain" key="bigContainRegister" v-else>
                    <div class="btitle">账户注册</div>
                    <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
                        <div class="bform">
                            <el-form-item>
                                <el-input class="elin" placeholder="用户名" v-model="ruleForm.username"></el-input>
                            </el-form-item>
                            <el-form-item>
                                <el-input class="elin" placeholder="密码" v-model="ruleForm.password" show-password>
                                </el-input>
                            </el-form-item>
                        </div>
                        <el-button class="bbutton">注册</el-button>
                    </el-form>
                </div>
            </div>
            <div class="small-box" :class="{ active: isLogin }">
                <div class="small-contain" key="smallContainRegister" v-if="isLogin">
                    <div class="stitle">你好，朋友!</div>
                    <p class="scontent">开始注册，和我们一起旅行</p>
                    <el-button class="sbutton" @click="changeType">注册</el-button>
                </div>
                <div class="small-contain" key="smallContainLogin" v-else>
                    <div class="stitle">欢迎回来!</div>
                    <p class="scontent">与我们保持联系，请登录你的账户</p>
                    <button class="sbutton" @click="changeType">登录</button>
                </div>
            </div>
        </el-container>
    </div>
</template>
<script>
export default {
    name: "Login",
    data() {
        return {
            isLogin: true,
            ruleForm: {
                username: '',
                password: '',
                useremail: ''
            },
            rules: {
                username: [
                    { required: true, message: '请输入用户名称', trigger: 'blur' },
                    { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }

                ],
                password: [
                    { required: true, message: '请选择密码', trigger: 'blur' }
                ]
            }
        };
    },
    methods: {
        changeType() {
            this.isLogin = !this.isLogin
            this.ruleForm.username = ''
            this.ruleForm.useremail = ''
            this.ruleForm.password = ''
        },
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    const _this = this;//获取整个vue的this
                    console.log("校验成功")
                    this.$axios.post('http://localhost:8081/login', this.ruleForm).then((res) => {
                        console.log(res)
                        const jwt = res.headers['authorization'];
                        const userInfo = res.data.data
                        // 把数据共享出去
                        // 存储(共享)全局变量jwt和userInfo
                        _this.$store.commit("SET_TOKEN", jwt);
                        _this.$store.commit("SET_USERINFO", userInfo);
                        _this.$router.push("/blogs")
                        //获取token和getUser
                        // console.log(_this.$store.getters.getToken)
                        // console.log(_this.$store.getters.getUser) 
                    })
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    }
}
</script>

<style scoped>
div {
    animation: fadeInAnimation ease 0.5s;
    animation-iteration-count: 1;
    animation-fill-mode: forwards;
}

@keyframes fadeInAnimation {
    0% {
        opacity: 0;
    }

    100% {
        opacity: 1;
    }
}

.login-register {
    width: 100%;
    height: 97vh;
    box-sizing: border-box;
    background-color: #bfffe4;
    background-image: linear-gradient(19deg, #bfffe4 0%, #74eee1 100%);
}

.contain {
    width: 60%;
    height: 60%;
    position: relative;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    border-radius: 20px;
    box-shadow: 0 0 3px #f0f0f0,
        0 0 6px #f0f0f0;
}

.big-box {
    width: 70%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 30%;
    transform: translateX(0%);
    transition: all 1s;
}

.big-contain {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

.btitle {
    margin-bottom: 10px;
    font-size: 1.5em;
    font-weight: bold;
    color: rgb(57, 167, 176);
}

.bform {
    width: 100%;
    height: 40%;
    padding: 2em 0;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    align-items: center;
}

.elin {
    width: 300px;
}

.bform .errTips {
    display: block;
    width: 50%;
    text-align: left;
    color: red;
    font-size: 0.7em;
    margin-left: 1em;
}

.bform input {
    width: 50%;
    height: 30px;
    border: none;
    outline: none;
    border-radius: 10px;
    padding-left: 2em;
    background-color: #f0f0f0;
}

.bbutton {
    margin-top: 10px;
    width: 100%;
    height: 40px;
    border-radius: 24px;
    border: none;
    outline: none;
    background-color: rgb(57, 167, 176);
    color: #fff;
    font-size: 0.9em;
    cursor: pointer;
}

.small-box {
    width: 30%;
    height: 100%;
    background: linear-gradient(135deg, rgb(57, 167, 176), rgb(56, 183, 145));
    position: absolute;
    top: 0;
    left: 0;
    transform: translateX(0%);
    transition: all 1s;
    border-top-left-radius: inherit;
    border-bottom-left-radius: inherit;
}

.small-contain {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

.stitle {
    font-size: 1.5em;
    font-weight: bold;
    color: #fff;
}

.scontent {
    font-size: 0.8em;
    color: #fff;
    text-align: center;
    padding: 2em 4em;
    line-height: 1.7em;
}

.sbutton {
    width: 60%;
    height: 40px;
    border-radius: 24px;
    border: 1px solid #fff;
    outline: none;
    background-color: transparent;
    color: #fff;
    font-size: 0.9em;
    cursor: pointer;
}

.big-box.active {
    left: 0;
    transition: all 0.5s;
}

.small-box.active {
    left: 100%;
    border-top-left-radius: 0;
    border-bottom-left-radius: 0;
    border-top-right-radius: inherit;
    border-bottom-right-radius: inherit;
    transform: translateX(-100%);
    transition: all 1s;
}

.el-header,
.el-footer {
    background-color: #b3c0d1;
    color: #333;
    text-align: center;
    line-height: 60px;
}

.el-main {
    /* background-color: #e9eef3; */
    color: #333;
    text-align: center;
    line-height: 160px;
}

.mlogo {
    height: 80%;
}

.demo-ruleForm {
    max-width: 500px;
    margin: 0 auto;
}
</style>
